﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aga.GestionDeArchivos.Domain.Entities;
using Aga.GestionDeArchivos.Storage;
using Aga.GestionDeArchivos.Common;

namespace Aga.GestionDeArchivos.Services
{
    public class ItemsService : DomainServiceBase<Item>
    {
        protected override RepositoryBase<Item> Repository
        {
            get { return RepositoryFactory.CreateItemRepository(); }
        }
        /// <summary>
        /// Get all Items
        /// </summary>
        /// <returns></returns>
        public IQueryable<Item> GetAll(string buscar)
        {
            return from i in base.GetAll()
                   where i.Detalle.ToLower().Contains(buscar.ToLower().Trim())
                   select i;
        }

        public PagedList<Item> GetPage(int pageIndex, int pageSize)
        {
            return GetPage(GetAll(), pageIndex, pageSize);
        }

        public PagedList<Item> GetPage(IQueryable<Item> allItems, int pageIndex, int pageSize)
        {
            int count = GetAll().Count();
            var items = allItems.OrderBy(c => c.Detalle).Skip(pageIndex * pageSize).Take(pageSize).ToList();

            int pageCount = Convert.ToInt32(Math.Ceiling((double)count / pageSize));

            return new PagedList<Item>(items, pageIndex, pageCount, pageSize, count);
        }
    }
}
